package com.chixing.service.impl;

import com.chixing.pojo.MyOrder;
import com.chixing.service.OrderService;
import com.chixing.service.ProductClient;
import com.chixing.util.JsonUtil;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * @author 马博
 * * @date 2021/10/18
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private ProductClient productClient;

    @Override
    public Object save(Integer userId,Integer proId) {
        //1.创建订单对象
        MyOrder myOrder=new MyOrder();
        //2.订单对象初始化属性(编号,商品名称,商品价格,用户id,下单时间)
        myOrder.setOrderId(UUID.randomUUID().toString());
        myOrder.setOrderNum(UUID.randomUUID().toString());
        myOrder.setUserId(userId);
        myOrder.setCreateTime(new Date());
        //商品名称,商品价格 --->调用商品服务
        String productStr=productClient.getById(proId);
        if (productStr!=null) {
            System.out.println("productStr:" + productStr);
            JsonNode jsonNode=JsonUtil.stringToJson(productStr);
            String proName = jsonNode.get("proName").toString();
            Float proPrice = Float.valueOf(jsonNode.get("proPrice").toString());
            myOrder.setProName(proName);
            myOrder.setProPrice(proPrice);
            //3.持久化到DB
            return myOrder;
        }else {
            System.out.println("不存在该商品的id");
            return null;
        }
    }
}
